package com.slz3.business.module.configManagement.conIndexVideo.steward;

import com.slz3.business.frame.steward.FindJoinSteward;
import com.slz3.business.module.configManagement.conIndexVideo.domain.ConIndexVideo;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.MultiValueMap;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

public class ConIndexVideoSpecification {

    public static FindJoin findJoin(MultiValueMap<String, String> params) {
        return new FindJoin(params);
    }

    private static class FindJoin implements Specification<ConIndexVideo> {

        private MultiValueMap<String, String> params;

        public FindJoin(MultiValueMap<String, String> params) {
            this.params = params;
        }

        @Override
        public Predicate toPredicate(Root<ConIndexVideo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {

            final List<Predicate> predicateList = new ArrayList<>();

            predicateList.addAll(FindJoinSteward.appoint(root, cb, ConIndexVideo.class, params));

//            predicateList.add(cb.equal(root.get("validFlag").as(ValidFlag.class), ValidFlag.VALID));

            return predicateList.size() > 0 ? cb.and(predicateList.toArray(new Predicate[]{})) : null;

        }

    }

}
